/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file creates the TWFE ATT estimates for the period when the pre-test
	passes (1996-2005). These results are displayed in Table D.3.
*/

/* NOTE - Global macros for directories are created in the appendices_results.do 
	file which calls this individual .do file */

********* Flood to CP or LEPA - 1996 to 2005, for Table D.3 *********
import delimited using "$dr_temp\flood_cporlepa_prepped.csv", clear
rename (wr_group jan_april_mean_et0_elev may_sep_mean_et0_elev) (WR_GROUP jan_april_mean_ET0_elev may_sep_mean_ET0_elev)
*Limit to 1996-2005
keep if inrange(wua_year, 1996, 2005) 
xtset WR_GROUP wua_year
xtdescribe
	putexcel set "$dr_temp\twfe.xlsx", ///
		sheet("floodtocplepa_96_05_nyt") modify
	putexcel A1 = "dep_var"
	putexcel B1 = "effect estimated"
	putexcel C1 = "estimate"
	putexcel D1 = "se_estimate"
	putexcel E1 = "lb_estimate"
	putexcel F1 = "ub_estimate"
	putexcel G1 = "panel_mean_dep_var"
	putexcel H1 = "scaled_estimate"
	putexcel I1 = "scaled_lb"
	putexcel J1 = "scaled_ub"
	local i = 2
	foreach y in "af_used"  "acres_irr" "depth_applied" {
		putexcel A`i' = "`y'"
		putexcel B`i' = "effect_average"
		sum `y'
		local var_mean = r(mean)
			putexcel G`i' = `var_mean'
		xtreg `y' treat_status ///
			jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_ET0_elev may_sep_mean_ET0_elev i.wua_year ///
			, fe vce(cluster WR_GROUP)
		putexcel C`i' = _b[treat_status]
		putexcel D`i' = _se[treat_status]
		local ++i
	}

********* CP to LEPA - 1996 to 2005, for Table D.3 *********
import delimited using "$dr_temp\cp_lepa_prepped.csv", clear
rename (wr_group jan_april_mean_et0_elev may_sep_mean_et0_elev) (WR_GROUP jan_april_mean_ET0_elev may_sep_mean_ET0_elev)
*Limit to 1996-2005
keep if inrange(wua_year, 1996, 2005) 
xtset WR_GROUP wua_year
xtdescribe
	putexcel set "$dr_temp\twfe.xlsx", ///
		sheet("cplepa_96_05") modify
	putexcel A1 = "dep_var"
	putexcel B1 = "effect estimated"
	putexcel C1 = "estimate"
	putexcel D1 = "se_estimate"
	putexcel E1 = "lb_estimate"
	putexcel F1 = "ub_estimate"
	putexcel G1 = "panel_mean_dep_var"
	putexcel H1 = "scaled_estimate"
	putexcel I1 = "scaled_lb"
	putexcel J1 = "scaled_ub"
	local i = 2
	foreach y in "af_used"  "acres_irr" "depth_applied" {
		putexcel A`i' = "`y'"
		putexcel B`i' = "effect_average"
		sum `y'
		local var_mean = r(mean)
			putexcel G`i' = `var_mean'
		xtreg `y' treat_status ///
			jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_ET0_elev may_sep_mean_ET0_elev i.wua_year ///
			, fe vce(cluster WR_GROUP)
		putexcel C`i' = _b[treat_status]
		putexcel D`i' = _se[treat_status]
		local ++i
	}